// pages/info/birthdate.js
var util = require('../../utils/util');
var that;

const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1917; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
  avaurl:'../../images/ava.png',
  years: years,
  year: 1990 ,//date.getFullYear(),
  months: months,
  month: 2,
  days: days,
  day: 6,
  value: [73, 1, 5],
  },
  onLoad:function(){
    that = this;
  },
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },
  lastStep: function () {
    var nowQuestion = wx.getStorageSync('nowQuestion');
    var questionInfo = wx.getStorageSync('questionInfo') || [];
    if (questionInfo instanceof Array && questionInfo.length > 0) {
      questionInfo.pop();
    } else {
      questionInfo = [];
    }
    wx.setStorageSync('questionInfo', questionInfo);
    util.prevQuestion(nowQuestion.location);
  },
  nextStep: function () {
    var nowQuestion = wx.getStorageSync('nowQuestion');
    nowQuestion.value = that.data.year + "-" + that.formatZero(that.data.month) + "-" + that.formatZero(that.data.day);
    var questionInfo = wx.getStorageSync('questionInfo') || [];
    if (questionInfo instanceof Array) {
      questionInfo.push(nowQuestion);
    } else {
      var arr = [];
      arr.push(questionInfo);
      arr.push(nowQuestion);
      questionInfo = arr;
    }
    wx.setStorageSync('questionInfo', questionInfo);
    util.nextQuestion(nowQuestion.location);
  },
  formatZero:function(v){
    if(v==undefined) return '';
    if(v.length==1){
      return "0"+v;
    }
    return v;
  }
})